package com.bobi.tfca.ui.adapter

import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.EditText
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.bobi.tfca.R
import com.bobi.tfca.databinding.ItemWriteArticleParagraphBinding
import com.bobi.tfca.network.response.ParagraphResponse
import com.bobi.tfca.ui.article.WriteArticleFragment
import com.bumptech.glide.Glide

class WriteParagraphAdapter(
    val fragment: WriteArticleFragment,
    var paragraphList: ArrayList<ParagraphResponse>
) :
    RecyclerView.Adapter<WriteParagraphAdapter.ParagraphViewHolder>() {


    private var mOnItemClickListener: OnItemClickListener? = null

    interface OnItemClickListener{
        fun onItemClick(imageView: ImageView, position: Int)
    }

    fun setOnItemClickListener(onItemClickListener: OnItemClickListener?) {
        mOnItemClickListener = onItemClickListener
    }

    inner class ParagraphViewHolder(private val binding: ItemWriteArticleParagraphBinding) : RecyclerView.ViewHolder(binding.root) {
        private val editText : EditText = binding.editText
        private val imageView: ImageView = binding.imageView

        init {
            editText.addTextChangedListener(object: TextWatcher{
                override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                }

                override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                }

                override fun afterTextChanged(p0: Editable?) {
                    val position = adapterPosition
                    if (position != RecyclerView.NO_POSITION) {
                        val paragraph = paragraphList[position]
                        paragraph.plainText = p0.toString()
                    }
                }
            })
        }

        fun bind(position: Int) {
            val plainText = paragraphList[position].plainText
            if (plainText.isNotBlank()) {
                editText.setText(plainText)
            } else {
                editText.setText("")
            }
            val url = paragraphList[position].picture
            if (url.isNotBlank() && (!"".equals(url))) {
                Glide.with(fragment).load(url)
                    .placeholder(R.drawable.defaultbg_h).into(imageView)
            } else {
                imageView.setImageResource(R.drawable.defaultbg_h)
            }
            imageView.setOnClickListener {
                mOnItemClickListener?.onItemClick(imageView, position)
            }
        }

    }

    fun addParagraph() {
        val paragraph = ParagraphResponse()
        paragraph.sort = getItemCount() + 1
        paragraphList.add(paragraph)
        notifyItemInserted(paragraphList.size - 1)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ParagraphViewHolder {
        val itemView = ItemWriteArticleParagraphBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        val viewHolder = ParagraphViewHolder(itemView)
        return viewHolder
    }

    override fun getItemCount(): Int = paragraphList.size

    override fun onBindViewHolder(holder: ParagraphViewHolder, position: Int) {
        holder.bind(position)
    }
}